package cn.tedu.loginsaltweb.controller;

import cn.tedu.loginsaltweb.pojo.dto.UserDTO;
import cn.tedu.loginsaltweb.response.JsonResult;
import cn.tedu.loginsaltweb.response.StatusCode;
import cn.tedu.loginsaltweb.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("register")
    public JsonResult register(@RequestBody UserDTO userDTO){
        if(userDTO == null){
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
        userService.register((userDTO));

        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping("login")
    public JsonResult login(@RequestBody UserDTO userDTO){
        if(userDTO == null){
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
        Boolean login = userService.login(userDTO);
        if(!login){
            return new JsonResult(StatusCode.OPERATION_FAIL);
        }
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
}
